﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StockFinder.Services.Extractors.IBD;
using StockFinder.Model;
using StockFinder.DataAccess;
using StockFinder.Services.Extractors;
using StockFinder.Services.Extractors.Yahoo;
using log4net;
using StockFinder.Model.Enumerations;
using StockFinder.Services.PriceAnalysis;
using System.IO;
using System.Net.Mail;
using System.Net;
using StockFinder.Services.Extractors.MarketSmith;
using StockFinder.Services.PriceAnalysis.Weekly;
using StockFinder.Services.Engines;
using System.Threading;
using StockFinder.Services.StockListFilter.IBD;
using StockFinder.StockLists;
using StockFinder.StockLists.Indexes;

namespace StockFinder.ConsoleHarness
{
    class Program
    {
        private static readonly ILog _Log = LogManager.GetLogger(typeof(Program));

        static void Main(string[] args)
        {
            #region Local variables
            GeneralDataAccess _GeneralDataAccess = new GeneralDataAccess();
            SymbolDataAccess _SymbolDataAccess = new SymbolDataAccess();
            DailyPriceDataAccess _DailyPriceDataAccess = new DailyPriceDataAccess();
            ResultsDataAccess _ResultsDataAccess = new ResultsDataAccess();
            IHistoricPricesExtractor _YahooricesExtractor = new YahooHistoricPriceExtractor();
            IBDDataAccess _IBDDataAccess = new IBDDataAccess();
            DateTime runDate = DateTime.Today; 
            #endregion

            /* CHECK ARGUMENTS */

            //-ibdratings -pricedb -updatesymbols -analysis -saveanalysis

            _Log.DebugFormat("Application will be run with the following parameters...");

            bool downloadIBDRatingsFromServer = args.Contains("-ibdratings");
            _Log.DebugFormat("Download IBD from server: {0}", downloadIBDRatingsFromServer);

            bool downloadPriceHistory = args.Contains("-pricedb");
            _Log.DebugFormat("Download Price History Database: {0}", downloadPriceHistory);

            bool updateSymbolData = args.Contains("-updatesymbols");
            _Log.DebugFormat("Update Symbol datatable: {0}", updateSymbolData);

            bool runPriceAnalysers = args.Contains("-analysis");
            _Log.DebugFormat("Run Price Analysers: {0}", runPriceAnalysers);

            bool savePriceAnalysersOutput = args.Contains("-saveanalysis");
            _Log.DebugFormat("Save Price Analysers output: {0}", savePriceAnalysersOutput);

            for (int i = 15; i > 0; i--)
            {
                Console.Write("\rStarting in {0} seconds ", i);
                Thread.Sleep(1000);
            }

            Console.WriteLine();            

            /* RUN IBD RATINGS UPDATER */

            _Log.DebugFormat("Running IBDRatingDatabaseUpdater...");

            IBDRatingDatabaseUpdater ibdRatingDatabaseUpdater = new IBDRatingDatabaseUpdater();
            if (downloadIBDRatingsFromServer)
            {
                _Log.DebugFormat("Downloading IBD Ratings from server");
                ibdRatingDatabaseUpdater.Run();

                //something happened so load last file
                if (Cache.IBDRatingsList == null)
                {
                    _Log.DebugFormat("Loading IBD Ratings from file as download latest failed");

                    ibdRatingDatabaseUpdater.LoadFromFile();
                }
            }
            else
            {
                _Log.DebugFormat("Loading IBD Ratings from file");
                ibdRatingDatabaseUpdater.LoadFromFile();
            }                                              

            /* GET SYMBOLS UNIVERSE */

            _Log.DebugFormat("Updating symbols...");         

            var symbolsList = Cache.IBDRatingsList.Select(i => i.Symbol).ToList();

            var stockLists = new List<IStockList>{
                new TelegraphOnlineFtse100StockList(){ LoadPrices = false},
                new TelegraphOnlineFtse250StockList(){ LoadPrices = false},
                new FinvizOnlineDowJonesStockList(){ LoadPrices = false},
                new FinvizOnlineSnP500StockList(){ LoadPrices = false}};

            string name = string.Empty;

            foreach (var stockList in stockLists)
            {
                foreach (var symbol in stockList)
                {
                    name = symbol.Name;

                    if (symbol.Exchange == Exchange.LONDON)
                    {
                        name += ".L";
                    }

                    if (!symbolsList.Contains(name))
                    {
                        symbolsList.Add(name);
                    }
                }
            }            

            if (updateSymbolData)
            {
                _Log.DebugFormat("Updating symbol datatable");

                try
                {
                    SymbolDataAccess.TruncateSymbolTable();
                    SymbolDataAccess.InsertSymbols(symbolsList);
                }
                catch (Exception exception)
                {
                    _Log.ErrorFormat("An unexpected error occurred updating symbols list in database -> {0}", exception);
                }
            }
            else
            {
                _Log.DebugFormat("Skipping updating symbol datatable");
            }                        

            /* RUN PRICE HISTORY DATABASE UPDATER */

            if (downloadPriceHistory)
            {
                _Log.DebugFormat("Running PriceHistoryDatabaseUpdater");

                DailyPricesHistoryDatabaseUpdater priceDatabaseUpdater = new DailyPricesHistoryDatabaseUpdater();
                priceDatabaseUpdater.Run(symbolsList);
            }
            else
            {
                _Log.Warn("Skipping download price history database");
            }            

            /* RUN PRICE ANALYSERS */

            if (runPriceAnalysers)
            {
                #region Price analysers local variables
                IBDRatings currentRatings = null;
                //HistoricPricesExtract result = null;
                List<DailyPrice> prices = null;
                IOrderedEnumerable<DailyPrice> orderedPrices = null;
                PriceAnalysisResult priceAnalysisResult = null;
                IPriceAnalyser hasJustBrokenOutPriceAnalyser = new JustBrokenOutPriceAnalyser();
                IPriceAnalyser withinWindowPriceAnalyser = new PriceWithinWindowPriceAnalyser();
                IPriceAnalyser biggestVolumeDayPriceAnalyser = new BiggestVolumeUpDayPriceAnalyser();
                IPriceAnalyser flatBasePriceAnalyser = new FlatBasePriceAnalyser();
                IPriceAnalyser threeWeeksTightPriceAnalyser = new ThreeWeeksTightPriceAnalyser();
                int progress = 0;
                StringBuilder brokenOutResultOutput = new StringBuilder();
                StringBuilder withinWindowResultOutput = new StringBuilder();
                StringBuilder biggestVolumeDayResultOutput = new StringBuilder();
                StringBuilder flatBaseResultOutput = new StringBuilder();
                StringBuilder threeWeeksTightResultOutput = new StringBuilder();

                var bounceFrom50SMAIBDFilterList = new BounceOff50SMAStockListFilter();
                var breakoutFilterList = new BreakoutStockListFilter();

                const string FLATBASE_COLUMNHEADERS = "Name, Basedepth, BaseLength, Composite, EPS, RS, SMR, AccDist, Grouprs, LastPriceDate, DaysOfPrices, PercentFromSMA50";
                const string THREEWEEKSTIGHT_COLUMNHEADERS = "Name, Composite, EPS, RS, SMR, AccDist, Grouprs, LastPriceDate, DaysOfPrices, PercentFromSMA50";

                brokenOutResultOutput.AppendLine("Symbolid, Name, Baselength, Basedepth, Composite, EPS, RS, SMR, AccDist, Grouprs, LastPriceDate, BaseLowDate,BaseLowPrice,PreviousHighDate, PreviousHighPrice, DaysOfPrices, PercentFromSMA50");
                withinWindowResultOutput.AppendLine("Symbolid, Name, Baselength, Basedepth, Composite, EPS, RS, SMR, AccDist, Grouprs, LastPriceDate, BaseLowDate,BaseLowPrice,PreviousHighDate, PreviousHighPrice, DaysOfPrices, DepthFromHigh, PercentFromSMA50");
                biggestVolumeDayResultOutput.AppendLine("Name, Basedepth, Composite, EPS, RS, SMR, AccDist, Grouprs, LastPriceDate, DaysOfPrices");
                flatBaseResultOutput.AppendLine(FLATBASE_COLUMNHEADERS);
                threeWeeksTightResultOutput.AppendLine(THREEWEEKSTIGHT_COLUMNHEADERS);
                #endregion

                #region Truncate price breach database
                try
                {
                    PriceBreachDataAccess.TruncatePriceBreachContender();
                }
                catch (Exception exception)
                {
                    _Log.ErrorFormat(string.Format("An unexpected error occurred truncating Price breach database --> {0}", exception));
                }
                #endregion

                for (int index = 0; index < Cache.IBDRatingsList.Count; index++)
                {
                    currentRatings = Cache.IBDRatingsList[index];

                    _Log.DebugFormat("Looking for {0} PriceHistory in database", currentRatings.Symbol);

                    prices = DailyPriceDataAccess.GetAllDailyPricesBySymbol(currentRatings.Symbol);

                    //if (result.HistoricPricesExtractResult == HistoricPricesExtractResult.SUCCESS)
                    if (prices != null)
                    {
                        _Log.DebugFormat("**** Found {0} prices ****", prices.Count);

                        //make sure we have at least 10 prices
                        if (prices != null && prices.Count > 10)
                        {
                            _Log.DebugFormat("Analysing {0} prices", prices.Count);

                            orderedPrices = prices.OrderByDescending(price => price.PriceDate);

                            #region Just broken out analyser

                            //find those that have just broken out 30 day base
                            priceAnalysisResult = hasJustBrokenOutPriceAnalyser.RunAnalysis(orderedPrices);

                            if (priceAnalysisResult.Succeeded)
                            {
                                _Log.DebugFormat("!!!! HAS JUST BROKEN OUT !!!!");

                                if (ApplicationConfiguation.SaveResults)
                                {
                                    try
                                    {
                                        _ResultsDataAccess.InsertJustBrokenOut(
                                           currentRatings.Symbol,
                                           priceAnalysisResult.BaseLength,
                                           priceAnalysisResult.BaseDepth,
                                           priceAnalysisResult.BaseLow,
                                           priceAnalysisResult.BaselowDate,
                                           priceAnalysisResult.PreviousHighDate,
                                           priceAnalysisResult.PreviousHigh,
                                           priceAnalysisResult.MostRecentPriceDate,
                                           priceAnalysisResult.DaysOfPrices,
                                           runDate);
                                    }
                                    catch (Exception exception)
                                    {
                                        _Log.ErrorFormat("An unexpected error occurred inserting JustBrokenOut result -- > {0}",
                                            exception);
                                    }
                                }

                                brokenOutResultOutput.AppendLine(string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},{16}",
                                    0,
                                    currentRatings.Symbol,
                                    priceAnalysisResult.BaseLength,
                                    priceAnalysisResult.BaseDepth,
                                    currentRatings.IBDComposite,
                                    currentRatings.EPS,
                                    currentRatings.RelativeStrength,
                                    currentRatings.SMR,
                                    currentRatings.AccDist,
                                    currentRatings.GroupRSRating,
                                    priceAnalysisResult.MostRecentPriceDate.ToString("dd/MM/yyyy"),
                                    priceAnalysisResult.BaselowDate.ToString("dd/MM/yyyy"),
                                    priceAnalysisResult.BaseLow,
                                    priceAnalysisResult.PreviousHighDate.ToString("dd/MM/yyyy"),
                                    priceAnalysisResult.PreviousHigh,
                                    priceAnalysisResult.DaysOfPrices,
                                    priceAnalysisResult.PercentFrom50SMA));
                            }

                            #endregion

                            #region Price within All time high window

                            priceAnalysisResult = withinWindowPriceAnalyser.RunAnalysis(orderedPrices);

                            if (priceAnalysisResult.Succeeded)
                            {
                                _Log.DebugFormat("&&&&& PRICE WITHIN BREAKOUT WINDOW &&&&&");

                                //update IBD stock list filters
                                bounceFrom50SMAIBDFilterList.FilterPriceAnalysisResult(priceAnalysisResult, currentRatings);

                                if (breakoutFilterList.FilterPriceAnalysisResult(priceAnalysisResult, currentRatings))
                                {
                                    PriceBreachDataAccess.InsertPriceBreachContender(currentRatings.Symbol,
                                        priceAnalysisResult.PreviousHigh, "All time high breakout");
                                }

                                if (ApplicationConfiguation.SaveResults)
                                {
                                    try
                                    {
                                        _ResultsDataAccess.InsertPriceWithinWindow(
                                            currentRatings.Symbol,
                                            priceAnalysisResult.BaseLength,
                                            priceAnalysisResult.BaseDepth,
                                            priceAnalysisResult.BaseLow,
                                            priceAnalysisResult.BaselowDate,
                                            priceAnalysisResult.PreviousHighDate,
                                            priceAnalysisResult.PreviousHigh,
                                            priceAnalysisResult.MostRecentPriceDate,
                                            priceAnalysisResult.DaysOfPrices,
                                            runDate);
                                    }
                                    catch (Exception exception)
                                    {
                                        _Log.ErrorFormat("An unexcpected error occurred insering PriceWithinWindow result --> {0}",
                                            exception);
                                    }

                                }

                                withinWindowResultOutput.AppendLine(string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},{16},{17}",
                                    0,
                                    currentRatings.Symbol,
                                    priceAnalysisResult.BaseLength,
                                    priceAnalysisResult.BaseDepth,
                                    currentRatings.IBDComposite,
                                    currentRatings.EPS,
                                    currentRatings.RelativeStrength,
                                    currentRatings.SMR,
                                    currentRatings.AccDist,
                                    currentRatings.GroupRSRating,
                                    priceAnalysisResult.MostRecentPriceDate.ToString("dd/MM/yyyy"),
                                    priceAnalysisResult.BaselowDate.ToString("dd/MM/yyyy"),
                                    priceAnalysisResult.BaseLow,
                                    priceAnalysisResult.PreviousHighDate.ToString("dd/MM/yyyy"),
                                    priceAnalysisResult.PreviousHigh,
                                    priceAnalysisResult.DaysOfPrices,
                                    priceAnalysisResult.DepthFromAllTimeHigh,
                                    priceAnalysisResult.PercentFrom50SMA));
                            }

                            #endregion

                            #region Flat base

                            priceAnalysisResult = flatBasePriceAnalyser.RunAnalysis(orderedPrices);

                            if (priceAnalysisResult.Succeeded)
                            {
                                _Log.DebugFormat("£££££ FLAT BASE £££££");

                                //"Name, Basedepth, BaseLength, Composite, EPS, RS, SMR, AccDist, Grouprs, LastPriceDate, DaysOfPrices, PercentFromSMA50";
                                flatBaseResultOutput.AppendLine(string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11}",
                                    currentRatings.Symbol,
                                    priceAnalysisResult.BaseDepth,
                                    priceAnalysisResult.BaseLength,
                                    currentRatings.IBDComposite,
                                    currentRatings.EPS,
                                    currentRatings.RelativeStrength,
                                    currentRatings.SMR,
                                    currentRatings.AccDist,
                                    currentRatings.GroupRSRating,
                                    priceAnalysisResult.MostRecentPriceDate.ToString("dd/MM/yyyy"),
                                    priceAnalysisResult.DaysOfPrices,
                                    priceAnalysisResult.PercentFrom50SMA));
                            }

                            #endregion

                            #region Three weeks tight

                            priceAnalysisResult = threeWeeksTightPriceAnalyser.RunAnalysis(orderedPrices);

                            if (priceAnalysisResult.Succeeded)
                            {
                                _Log.DebugFormat("^^^^ THREE WEEKS TIGHT ^^^^^");

                                //"Name, Basedepth, BaseLength, Composite, EPS, RS, SMR, AccDist, Grouprs, LastPriceDate, DaysOfPrices, PercentFromSMA50";
                                threeWeeksTightResultOutput.AppendLine(string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9}",
                                    currentRatings.Symbol,
                                    currentRatings.IBDComposite,
                                    currentRatings.EPS,
                                    currentRatings.RelativeStrength,
                                    currentRatings.SMR,
                                    currentRatings.AccDist,
                                    currentRatings.GroupRSRating,
                                    priceAnalysisResult.MostRecentPriceDate.ToString("dd/MM/yyyy"),
                                    priceAnalysisResult.DaysOfPrices,
                                    priceAnalysisResult.PercentFrom50SMA));
                            }

                            #endregion

                            #region Biggest volume up day

                            priceAnalysisResult = biggestVolumeDayPriceAnalyser.RunAnalysis(orderedPrices);

                            if (priceAnalysisResult.Succeeded)
                            {
                                _Log.DebugFormat("^^^^^ BIGGEST VOLUME UP DAY ^^^^^");

                                if (ApplicationConfiguation.SaveResults)
                                {
                                    try
                                    {
                                        _ResultsDataAccess.InsertBiggestVolumeUpDayResult(
                                            currentRatings.Symbol,
                                            priceAnalysisResult.DaysOfPrices,
                                            priceAnalysisResult.BaseDepth,
                                            priceAnalysisResult.MostRecentPriceDate,
                                            runDate);
                                    }
                                    catch (Exception exception)
                                    {
                                        _Log.ErrorFormat("An unexcpected error occurred insering BiggestVolumeUpDay result --> {0}",
                                            exception);
                                    }
                                }

                                withinWindowResultOutput.AppendLine(string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10}",
                                    0,
                                    currentRatings.Symbol,
                                    priceAnalysisResult.BaseDepth,
                                    currentRatings.IBDComposite,
                                    currentRatings.EPS,
                                    currentRatings.RelativeStrength,
                                    currentRatings.SMR,
                                    currentRatings.AccDist,
                                    currentRatings.GroupRSRating,
                                    priceAnalysisResult.MostRecentPriceDate.ToString("dd/MM/yyyy"),
                                    priceAnalysisResult.DaysOfPrices));
                            }

                            #endregion
                        }
                        else
                        {
                            _Log.Warn("No prices or count less than 10");
                        }
                    }
                    else
                    {
                        _Log.WarnFormat("Could not find prices for {0}", currentRatings.Symbol);
                    }

                    progress = Convert.ToInt32(((decimal)index / (decimal)Cache.IBDRatingsList.Count) * 100);
                    _Log.DebugFormat("{0}%", progress);                    
                }

                /* SAVE/EMAIL PRICE ANALYSERS RUN */

                if (savePriceAnalysersOutput)
                {
                    string resultsDirectory = ApplicationConfiguation.ResultsDirectory;

                    #region Write broken out file

                    string brokenOutFileName = string.Format(@"{0}JustBrokenOut\JustBrokenOut{1}.csv", resultsDirectory, DateTime.Today.ToString("ddMMyyyy"));

                    _Log.DebugFormat("Generating output file: {0}", brokenOutFileName);

                    try
                    {
                        if (!File.Exists(brokenOutFileName))
                        {
                            File.WriteAllText(brokenOutFileName, brokenOutResultOutput.ToString());
                        }
                    }
                    catch (Exception exception)
                    {
                        _Log.ErrorFormat(exception.ToString());
                        //write to local
                        brokenOutFileName = string.Format(@"JustBrokenOut{0}.csv", DateTime.Today.ToString("ddMMyyyy"));
                        File.WriteAllText(brokenOutFileName, brokenOutResultOutput.ToString());
                    }

                    #endregion

                    #region Write within window file

                    string withinWindowFileName = string.Format(@"{0}PriceWithinWindow\PriceWithinWindow{1}.csv", resultsDirectory, DateTime.Today.ToString("ddMMyyyy"));

                    _Log.DebugFormat("Generating output file: {0}", withinWindowFileName);

                    try
                    {
                        if (!File.Exists(withinWindowFileName))
                        {
                            File.WriteAllText(withinWindowFileName, withinWindowResultOutput.ToString());
                        }
                    }
                    catch (Exception exception)
                    {
                        _Log.ErrorFormat(exception.ToString());
                        withinWindowFileName = string.Format(@"PriceWithinWindow{0}.csv", DateTime.Today.ToString("ddMMyyyy"));
                        File.WriteAllText(withinWindowFileName, withinWindowResultOutput.ToString());
                    }

                    #endregion

                    #region Flat base file

                    string flatBaseFileName = string.Format(@"{0}FlatBaseDay\FlatBaseDay{1}.csv", resultsDirectory, DateTime.Today.ToString("ddMMyyyy"));

                    _Log.DebugFormat("Generating output file: {0}", flatBaseFileName);

                    try
                    {
                        if (!File.Exists(flatBaseFileName))
                        {
                            File.WriteAllText(flatBaseFileName, flatBaseResultOutput.ToString());
                        }
                    }
                    catch (Exception exception)
                    {
                        _Log.ErrorFormat(exception.ToString());
                        flatBaseFileName = string.Format(@"FlatBase{0}.csv", DateTime.Today.ToString("ddMMyyyy"));
                        File.WriteAllText(flatBaseFileName, flatBaseResultOutput.ToString());
                    }

                    #endregion

                    #region Biggest volume file

                    string biggestVolumeUpDayFileName = string.Format(@"{0}BiggestUpVolumeDay\BiggestUpVolumeDay{1}.csv", resultsDirectory, DateTime.Today.ToString("ddMMyyyy"));

                    _Log.DebugFormat("Generating output file: {0}", biggestVolumeUpDayFileName);

                    try
                    {
                        if (!File.Exists(biggestVolumeUpDayFileName))
                        {
                            File.WriteAllText(biggestVolumeUpDayFileName, biggestVolumeDayResultOutput.ToString());
                        }
                    }
                    catch (Exception exception)
                    {
                        _Log.ErrorFormat(exception.ToString());
                        biggestVolumeUpDayFileName = string.Format(@"BiggestUpVolumeDay{0}.csv", DateTime.Today.ToString("ddMMyyyy"));
                        File.WriteAllText(biggestVolumeUpDayFileName, biggestVolumeDayResultOutput.ToString());
                    }

                    #endregion

                    #region Three weeks tight file

                    string threeWeeksTightFileName = string.Format(@"{0}ThreeWeeksTight\ThreeWeeksTight{1}.csv", resultsDirectory, DateTime.Today.ToString("ddMMyyyy"));

                    _Log.DebugFormat("Generating output file: {0}", threeWeeksTightFileName);

                    try
                    {
                        if (!File.Exists(threeWeeksTightFileName))
                        {
                            File.WriteAllText(threeWeeksTightFileName, threeWeeksTightResultOutput.ToString());
                        }
                    }
                    catch (Exception exception)
                    {
                        _Log.ErrorFormat(exception.ToString());
                        threeWeeksTightFileName = string.Format(@"ThreeWeeksTight{0}.csv", DateTime.Today.ToString("ddMMyyyy"));
                        File.WriteAllText(threeWeeksTightFileName, threeWeeksTightResultOutput.ToString());
                    }

                    #endregion

                    #region Emailing results

                    _Log.DebugFormat("Emailing results");
                    try
                    {
                        SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
                        client.EnableSsl = true;
                        MailAddress from = new MailAddress("ssattin@gmail.com", "Simon Sattin");
                        MailMessage message = new MailMessage(from, from);
                        message.Body = "Stock Finder Results";
                        message.Subject = "Stock Finder Results";
                        NetworkCredential myCreds = new NetworkCredential("ssattin@gmail.com", "bergkamp_10", "");
                        client.Credentials = myCreds;

                        //add attachments results
                        message.Attachments.Add(new Attachment(brokenOutFileName));
                        message.Attachments.Add(new Attachment(withinWindowFileName));
                        message.Attachments.Add(new Attachment(biggestVolumeUpDayFileName));
                        message.Attachments.Add(new Attachment(flatBaseFileName));
                        message.Attachments.Add(new Attachment(threeWeeksTightFileName));

                        client.Send(message);
                    }
                    catch (Exception ex)
                    {
                        _Log.ErrorFormat(ex.ToString());
                    }

                    #endregion

                    #region Update IBD StockLists
                    try
                    {
                        breakoutFilterList.UpdateServerList();
                    }
                    catch (Exception exception)
                    {
                        _Log.ErrorFormat("An unexpected error occurred updating breakout list on IBD server --> {0}",
                            exception.ToString());
                    }

                    try
                    {
                        bounceFrom50SMAIBDFilterList.UpdateServerList();
                    }
                    catch (Exception exception)
                    {
                        _Log.ErrorFormat("An unexpected error occurred updating bounce off 50 SMA list on IBD server --> {0}",
                            exception.ToString());
                    }                    
                    #endregion
                }
                else
                {
                    _Log.Warn("Skipping saving of price analysis output");
                }
            }
            else
            {
                _Log.Warn("Skipping PriceAnalysers run");
            }            

            _Log.DebugFormat("Completed");
        }
    }
}
